#include <iostream>
using namespace std;

/*
    2.Aipoint类重载[]，要求类对象可以使用中括号获取下标i对应的值
      Aipoint<int> al(5);
      al[0]访问下标0的元素值
*/

template <typename T>
class Aipoint
{
public:
    Aipoint(int len)
    {
        this->len = len;
        p = new T[len];
    }

    ~Aipoint()
    {
        delete[] p;
    }

    void setValue(int index, T value)
    {
        if (index >= 0 && index < len)
        {
            p[index] = value;
        }
        else
        {
            cout << "index error" << endl;
        }
    }

    void getValue(int index)
    {
        if (index >= 0 && index < len)
        {
            cout << p[index] << endl;
        }
        else
        {
            cout << "index error" << endl;
        }
    }

    T &operator[](int index)
    {
        if (index >= 0 && index < len)
        {
            return p[index];
        }
        else
        {
            throw std::out_of_range("index error");
        }
    }

private:
    int len;
    T *p;
};

int main()
{
    Aipoint<int> a(10);
    for (int i = 0; i < 10; i++)
    {
        a.setValue(i, i + 1);
    }
    
    cout << a[5] << endl;

    a[5] = 100;
    cout << a[5] << endl;
}